<template>
  <div class="app-container calendar-list-container">
    <el-button class="filter-item" type="primary" @click="cancel">返回</el-button>
    <el-button class="filter-item" type="success" @click="addGoods">添加商品</el-button>
    <el-button class="filter-item" type="primary" @click="save">保存</el-button>
    <div class="w-body">
      <el-form :model="entity" :rules="dataRule" ref="entity" label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="entity.customerName" :readonly="true">
                <el-button slot="append" icon="el-icon-search" @click="addCustomer"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="仓库列表" prop="warehouseCode">
              <el-select v-model="entity.warehouseCode" placeholder="请选仓库" @change="doChange">
                <el-option v-for="(type, index) in warehouseList" :label="type.warehouseName" :value="type.warehouseCode" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="到货时间" prop="sendTime">
              <el-date-picker v-model="entity.sendTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="付款方式" prop="payType">
              <el-input v-model="entity.payType">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="运输方式" prop="transport">
              <el-input v-model="entity.transport">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="运输费用" prop="transportFee">
              <el-input v-model="entity.transportFee">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="币种" prop="currency">
              <el-select v-model="entity.currency" :disabled="view" placeholder="币种">
                <el-option v-for="(type,index) in currencyList" :label="type.name" :value="type.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否含税" prop="tax">
              <el-select v-model="entity.tax" placeholder="是否含税">
                <el-option v-for="(type,index) in yesOrNo" :label="type.name" :value="type.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="" prop="remark">
              <quill-editor v-model="entity.remark" ref="quillEditor" style="height:300px" :options="{ modules:{ toolbar:[ ['bold', 'italic', 'underline', 'strike']]}}">
              </quill-editor>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="">
              <div class="w-table">
                <grid gridTitle="物品信息" :columns="columns" :loading="false" :enableEdit="true" ref="mainGrid" height="400" :pagination="false" @delete="doDelete"
                  :permissions="permissions" :keyEditStep="2"></grid>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <GridDialog ref="customerGridDialog" :url="customerUrl" :columns="customerColumns" @select="selectCustomerData" :permissions="permissions"
      :queryEntity="{customerType: 'CUSTOMER'}"></GridDialog>
    <GridDialog ref="goodsGridDialog" :multi="true" :url="goodsUrl" :columns="goodsColumns" @select="selectGoodsData" :permissions="permissions" :queryEntity="goodsQueryEntity"
      :queryList="modelQueryList"></GridDialog>
  </div>
</template>

<script>
import request from '@/router/axios'
import { Message, MessageBox } from 'element-ui'
import url from '@/const/apiURL'
import GridDialog from '@/components/GridDialog'
import grid from '@/components/Grid'
import { mapGetters } from 'vuex'
import { dict } from '@/const/dict'
import { queryField } from '@/const/query'

export default {
  components: { GridDialog, grid },
  data () {
    return {
      modelQueryList: queryField().modelName,
      currencyList: dict().Currency,
      yesOrNo: dict().YesOrNo,
      warehouseList: [],
      customerUrl: url.CUSTOMER_LIST_URL,
      goodsUrl: url.MODEL_LIST_URL,
      goodsQueryEntity: { warehouseCode: null },
      entity: {
        customerCode: '',
        customerName: '',
        goods: []
      },
      columns: [
        { label: this.global.modelName, width: 100, prop: 'goodsName', align: 'left' },
        { label: this.global.modelProp, width: 100, prop: 'modelProp', align: 'left' },
        { label: '物料名称', width: 160, prop: 'materialName', align: 'left' },
        { label: '物料类型', width: 80, prop: 'materialTypeName', align: 'center' },
        { label: '单位', width: 80, prop: 'unit', align: 'center' },
        //   { label: '序列号', width: 160, prop: 'serialNumber', align: 'center' },
        //  { label: '批次号', width: 160, prop: 'batchNo', align: 'center' },
        //  { label: '库存数量', width: 80, prop: 'inQuantity', align: 'center' },
        { label: '单价', width: 120, prop: 'price', align: 'center', template: 'edit', type: 'number' },
        { label: '数量', width: 120, prop: 'quantity', align: 'center', template: 'edit', type: 'number' },
        { label: '操作', width: 80, prop: 'op', align: 'center', template: 'op', opList: [{ name: '删除', clickName: 'delete', auth: 'yes' }] }
      ],
      goodsColumns: [
        { label: this.global.modelName, width: 100, prop: 'modelName', align: 'center' },
        { label: this.global.modelProp, width: 80, prop: 'modelProp', align: 'center' },
        { label: '物料名称', width: 370, prop: 'materialName', align: 'center' },
        { label: '物料类型', width: 100, prop: 'materialTypeName', align: 'center' },
        { label: '数量', width: 80, prop: 'quantity', align: 'center' },
        { label: '单位', width: 80, prop: 'unit', align: 'center' }
      ],
      customerColumns: [
        { label: '客户名称', width: 160, prop: 'customerName', align: 'center' },
        { label: '联系人', width: 160, prop: 'linkName', align: 'center' },
        { label: '联系电话', width: 160, prop: 'linkMoblie', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      dataRule: {
        customerName: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
        warehouseCode: [{ required: true, message: '仓库不能为空', trigger: 'blur' }],
        currency: [{ required: true, message: '币种不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.init()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    init () {
      this.initWarehouse()
    },
    cancel () {
      this.$router.push({ path: '/prod/sale', query: {} })
    },
    addCustomer () {
      this.$refs['customerGridDialog'].show()
    },
    selectCustomerData (row) {
      this.entity.customerName = row.customerName
      this.entity.customerCode = row.customerCode
    },
    addGoods () {
      if (this.entity.warehouseCode) {
        this.$refs['goodsGridDialog'].show()
      } else {
        MessageBox.alert("请选择仓库")
      }
    },
    doChange (value) {
      this.$refs['mainGrid'].deleteAll()
      this.goodsQueryEntity.warehouseCode = this.entity.warehouseCode
    },
    selectGoodsData (rows) {
      rows.forEach(row => {
        const rowData = {
          goodsCode: row.modelCode,
          goodsName: row.modelName,
          materialName: row.materialName,
          materialTypeName: row.materialTypeName,
          unit: row.unit,
          modelProp: row.modelProp,
          inQuantity: row.quantity,
          quantity: 0,
          price: 0,
          batchNo: row.batchNo,
          serialNumber: ''
        }
        this.$refs['mainGrid'].addRow(rowData)
      })
    },
    save () {
      let purchase = this.entity
      purchase.goods = this.$refs['mainGrid'].getData()
      this.saveUrl(purchase).then(data => {
        if (data) {
          this.$router.push({ path: '/prod/sale', query: {} })
        }
      })
    },
    doDelete (row) {
      this.$refs['mainGrid'].deleteRow(row)
    },
    initWarehouse () {
      this.warehouseUrl().then(data => {
        if (data) {
          this.warehouseList = data
        }
      })
    },
    saveUrl (data) {
      return request({
        url: url.SALE_ADD_URL,
        method: 'post',
        data: data
      })
    },
    warehouseUrl () {
      return request({
        url: url.WAREHOUSE_USABLE_URL,
        method: 'post',
        data: { key: 'key' }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.w-body {
  padding-top: 8px;
}
.w-table {
  border: 1px solid #ebeef5;
  margin-top: 50px;
}
.w-table-title {
  border: 1px solid #ebeef5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  line-height: 23px;
  display: inline-block;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: relative;
  vertical-align: middle;
  width: 100%;
  font-size: 14px;
  color: gray;
}
</style>
